/*
 * Grupo:		Danilo Beraldo Ireno
 * 				Flavio Augusto de Mello
 * 				Giuliano G. Maranha
 * 				Luis Fernando Junior
 *
 * Disciplina: 	Compiladores
 * Professor:	Dr. Eraldo Pereira Marinho*
 */


#include "asmgen.h"


void semantic_asm( int action ){

  switch(action){

  case BSSASM: make_bsssection();break;
  case DATASM: make_datasection();break;
  case TEXASM: make_textsection();break;
  case STARTASM: make_starsection();break;
  case LESSASM: geq_asm();break;
  case GREATASM: great_asm();break;
  case NEQASM: neq_asm();break;
  case LEQASM: leq_asm();break;
  case GEQASM: geq_asm();break;
  case INASM:  in_asm();break;
  case MULASM: multiply_asm();break;
  case DIVISASM: division_asm();break;
  case DIVASM: div_asm();break;
  case MODASM: mod_asm();break;
  case ANDASM: and_asm();break;
  case SUBASM: subtraction_asm();break;
  case ADDASM: addition_asm();break;
  case ORASM: or_asm();break;
  case UINTASM: uint_asm();break;

  }
}

void make_bsssection(){

	fprintf(p_File,"\n\t\t.section\t.bss\n");

}

void make_datasection(){

	fprintf(p_File,"\n\t\t.section\t.data\n");

}

void make_textsection(){
	fprintf(p_File,"\n\t\t.section\t.text\n");

}
void make_starsection(void){

	fprintf(p_File, ".global\t_start\n");
	fprintf(p_File, "\t.type\t_start,\t@function\n");
	fprintf(p_File, "_start:\n");
}


//isrelop
void less_asm(){

	fprintf(p_File,"\tcmp %%eax, %%esp\n");
	fprintf(p_File,"\tle\t%%eax\n");
}

void great_asm(){

	fprintf(p_File,"\tcmp %%eax, %%esp\n");
	fprintf(p_File, "\tgt\t%%eax\n");

}

void neq_asm(){

	fprintf(p_File,"\tcmp %%eax, %%esp\n");
	fprintf(p_File,"\tne\t%%eax\n");

}
void leq_asm(){

	fprintf(p_File,"\tcmp %%eax, %%esp\n");
	fprintf(p_File,"\tle\t%%eax\n");

}
void geq_asm(){

	fprintf(p_File,"\tcmp %%eax, %%esp\n");
	fprintf(p_File,"\tge\t%%eax\n");

}
void in_asm(){


}
//isrelop

//ismull

void multiply_asm(void){

		fprintf(p_File, "\tpopl\t%%edx\n");
		fprintf(p_File, "\tmull\t%%edx\n");
}

void division_asm(void){

		  fprintf(p_File,"\tmovl %%eax,%%ecx\n");
		  fprintf(p_File,"\tpopl %%eax\n");
		  fprintf(p_File,"\tcltd\n");
		  fprintf(p_File,"\tdivl %%ecx\n");
}

void div_asm(){


}

void mod_asm(){


}
void and_asm(){
	fprintf(p_File,"\tandb (%%esp),%%al\n");
	fprintf(p_File,"\taddl $4, (%%esp)\n");

}
//ismull

//isadd
void subtraction_asm(void){

	 fprintf(p_File,"\tsubl %%esp, %%eax\n");
	 fprintf(p_File,"\taddl $4, %%esp\n");

}
void addition_asm(){

	fprintf(p_File,"\taddl %%esp, %%eax\n");
	fprintf(p_File,"\taddl $4, %%esp\n");

}
void or_asm(){

	fprintf(p_File,"\torb (%%esp),%%al\n");
	fprintf(p_File,"\taddl $4, (%%esp)\n");

}

void uint_asm(void){

	fprintf(p_File,"\tpush %%eax\n");
	fprintf(p_File,"\tmovl $%s, %%eax\n",lexeme);

}
